<?php
/**
 * Create by Phpstorm
 * User: Peco
 * Date: 2019/12/14
 * Time: 11:33
 */

namespace App\Repositories;

use App\Models\Attachment;

/**
 * 附件管理资源类
 * Class AttachmentRepository
 * @package App\Repository
 */
class AttachmentRepository
{
    /**
     * @var Attachment
     */
    protected $attachment;
    /**
     * @var array
     */
    public $attributes;

    /**
     * AttachmentRepository constructor.
     * @param $attachment
     */
    public function __construct(Attachment $attachment)
    {
        $this->attachment = $attachment;
        $this->attributes = $attachment->attrAlias;
    }


    /**
     * @param $data
     * @return Attachment|bool
     */
    public function store($data)
    {
        $this->attachment->object_key = $data['object_key'];
        isset($data['file_name']) && $this->attachment->file_name = $data['file_name'];
        isset($data['location']) && $this->attachment->location = $data['location'];
        isset($data['duration']) && $this->attachment->duration = $data['duration'];
        isset($data['belong']) && $this->attachment->belong = $data['belong'];
        isset($data['belong_id']) && $this->attachment->belong_id = $data['belong_id'];

        if (!$this->attachment->save()) {
            return false;
        }

        return $this->attachment;
    }

    /**
     * 显示附件详情
     * @param $id
     * @return Attachment|bool
     */
    public function show($id)
    {
        $this->attachment = $this->attachment->find($id);
        if (!$this->attachment) {
            return false;
        }
        return $this->attachment;
    }

    /**
     * 删除附件
     * @param $id
     * @return bool|null
     */
    public function destroy($id)
    {
        $this->attachment = $this->attachment->find($id);
        if (!$this->attachment) {
            return false;
        }
        return $this->attachment->delete();
    }

    /**
     * 更新附件
     * 更新时 关联模型和关联主键都必须有值 主要更新这两个字段 其它字段系统无法操作
     * @param $data
     * @return Attachment|bool
     */
    public function update($id, $data)
    {
        if (!isset($id)) return false;
        if (!isset($data['belong'])) return false;
        if (!isset($data['belong_id'])) return false;

        $this->attachment = $this->attachment->find($id);

        $this->attachment->belong = $data['belong'];
        $this->attachment->belong_id = $data['belong_id'];
        if (!$this->attachment->save()) {
            return false;
        }
        return $this->attachment;
    }


    /**
     * 绑定与解绑附件 解绑后附件会在定时任务中被删除
     * @param $key //关联模型的主键
     * @param $modelName //关联模型名称 模型名称应该在常量中定义
     * @param $data //前端传过来的附件数组
     * @return bool
     */
    public function bindBelongs($key, $modelName, $data)
    {
        if ($data && is_array($data)) {
            foreach ($data as $item) {
                $attachment = $this->attachment->find($item['id']);
                if ($attachment) {
                    //删除绑定
                    if (data_get($item, 'flag') == ATTACHMENT_DELETE) {
                        $attachment->belong = '';
                        $attachment->belong_id = '';
                        if (!$attachment->save()) {
                            return false;
                        }
                    }
                    //关联绑定 默认
                    if (data_get($item, 'flag') != ATTACHMENT_DELETE) {
                        $attachment->belong = $modelName;
                        $attachment->belong_id = $key;
                        if (!$attachment->save()) {
                            return false;
                        }
                    }
                }
            }
        }
        if ($data && is_numeric($data)) {
            $attachment = $this->attachment->find($data);
            if ($attachment) {
                //关联绑定
                $attachment->belong = $modelName;
                $attachment->belong_id = $key;
                return $attachment->save();
            }
        }
        return true;
    }

    /**
     * 解除与附件的绑定  主要是后台删除时调用
     * @param $key //数组 或 整型数字
     * @param $modelName //关联的模型名称
     * @return mixed
     */
    public function unBindBelongs($key, $modelName)
    {
        if (is_array($key)) {
            return $this->attachment->where('belong', $modelName)
                ->whereIn('belong_id', $key)//[1, 2, 3] 数组类型
                ->update(['belong' => '', 'belong_id' => 0]);
        }
        if (is_numeric($key)) {
            return $this->attachment->where('belong', $modelName)
                ->where('belong_id', $key)// 2 一个整数
                ->update(['belong' => '', 'belong_id' => 0]);
        }
    }
}
